package com.ccb.sc.member.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//psl import com.ccb.sc.common.bind.exception.BizException;
import com.ccb.sc.common.code.BizCodeFace;
import com.ccb.sc.common.code.ErrorCode;
import com.ccb.sc.common.modules.member.service.MemberService;
import com.ccb.sc.common.modules.setting.service.ISettingsService;
import com.ccb.sc.common.modules.setting.vo.ShopkeeperApplySettingsVo;
import com.ccb.sc.common.modules.shopkeeper.entity.Shopkeeper;
import com.ccb.sc.common.modules.shopkeeper.entity.ShopkeeperApply;
import com.ccb.sc.common.modules.shopkeeper.enums.ApplyStatus;
import com.ccb.sc.common.modules.shopkeeper.enums.ShopkeeperLevel;
import com.ccb.sc.common.modules.shopkeeper.mapper.ShopkeeperApplyMapper;
import com.ccb.sc.common.modules.shopkeeper.service.ShopkeeperService;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperApplyCheckVo;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperTeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Optional;

/**
 * @author ken
 * @version 1.0
 * @date 2023/2/13 0:29
 */
@Service
public class HeadsApplyService extends ServiceImpl<ShopkeeperApplyMapper, ShopkeeperApply> {

    @Autowired
    private ShopkeeperService shopkeeperService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ISettingsService settingsService;

    /**
     * 团长申请
     * @param memberId 会员id
     */
    public void apply(Long memberId) {
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(memberId);
        if (shopkeeper != null && shopkeeper.getLevel().getValue() > 0) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("已成为团长，无需重复申请！"));
        }
        if (shopkeeper == null) {
            shopkeeper = this.shopkeeperService.create(memberId);
        }
        Long shopkeeperId = shopkeeper.getId();
        // 判断是否存在申请中的记录
        long count = this.count(Wrappers.<ShopkeeperApply>lambdaQuery()
                .eq(ShopkeeperApply::getShopkeeperId, shopkeeperId)
                .eq(ShopkeeperApply::getStatus, ApplyStatus.WAIT));
        Assert.isTrue(count <= 0, "已申请，请耐心等待审核！");

        ShopkeeperApply apply = new ShopkeeperApply();
        apply.setShopkeeperId(shopkeeperId);

        ShopkeeperTeamVo team = this.shopkeeperService.team(shopkeeperId);
        ShopkeeperApplySettingsVo settings = this.settingsService.getSettings(ShopkeeperApplySettingsVo.class);
        // 校验条件
        int level4Team = Optional.of(settings.getLevel4Team()).orElse(3);
        if (team.getTeams() < level4Team) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("需邀请" + level4Team + "人注册"));
        }
        apply.setTeam(team.getTeams());
        apply.setLevel(ShopkeeperLevel.HEADS);
        apply.setTotalAmount(team.getAmount());
        this.save(apply);
    }

    public ShopkeeperApplyCheckVo check(Long memberId) {
        ShopkeeperApplyCheckVo vo = new ShopkeeperApplyCheckVo();
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(memberId);
        if (shopkeeper == null) {
            shopkeeper = this.shopkeeperService.create(memberId);
        }
        Long shopkeeperId = shopkeeper.getId();
        ShopkeeperApplySettingsVo settings = this.settingsService.getSettings(ShopkeeperApplySettingsVo.class);
        ShopkeeperTeamVo team = this.shopkeeperService.team(shopkeeperId);

        int level4Team = Optional.of(settings.getLevel4Team()).orElse(3);
        vo.setMatch(team.getInvites() >= level4Team);
        vo.setTeam(team.getInvites());
        vo.setTotalTeam(level4Team);
        vo.setAmount(team.getAmount());
        vo.setTotalAmount(BigDecimal.ZERO);
        return vo;
    }

    public ApplyStatus queryApply(Long memberId) {
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(memberId);
        if (shopkeeper == null) {
            shopkeeper = this.shopkeeperService.create(memberId);
        }
        Long shopkeeperId = shopkeeper.getId();
        ShopkeeperApply shopkeeperApply = this.getOne(Wrappers.<ShopkeeperApply>lambdaQuery()
                .eq(ShopkeeperApply::getShopkeeperId, shopkeeperId)
                .orderByDesc(ShopkeeperApply::getId).last("limit 1"));
        return shopkeeperApply == null ? null : shopkeeperApply.getStatus();
    }
}
